import pandas as pd
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# 加载数据
data = pd.read_csv('gpt.csv')

# 将日期字符串转换为datetime对象
data['date'] = pd.to_datetime(data['date'])

# 从日期中提取特征
data['year'] = data['date'].dt.year
data['month'] = data['date'].dt.month
data['day'] = data['date'].dt.day

# 特征和目标变量
X = data[['year', 'month', 'day', 'weight', 'height', 'blood_glucose', 'physical_activity', 'diet', 'medication_adherence', 'stress_level', 'sleep_hours', 'hydration_level']]
y = data['risk_score']

# 划分训练集和测试集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# 初始化决策树模型
dt_model = DecisionTreeRegressor(random_state=42)

# 训练模型
dt_model.fit(X_train, y_train)

# 进行预测
y_pred_dt = dt_model.predict(X_test)

# 绘制图表的代码保持不变...